package com.dragon.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 注册与取消注册驱动案例
 *
 * @author dragon
 * @since 2019/9/25 00:37
 */
public class RegisterAndDeRegisterDriverDemo {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 加载Driver class字节码文件，调用其静态代码块，创建一个MysqlDriver注册到DriverManager里面去
        Class.forName("com.mysql.jdbc.Driver");
        // 定义连接的 url
        String url = "jdbc:mysql://localhost/mybatis_test";
        // 通过DriverManager 获得注册的驱动，原理是通过Driver.accepts(url) 方法判断来判断，来获取驱动
        Driver driver = DriverManager.getDriver(url);
        // 通过DriverManager 获取连接对象
        Connection connection = DriverManager.getConnection(url, "root", "dragon4369");
        System.out.println("connection:" + connection);
        // 取消注册驱动
        DriverManager.deregisterDriver(driver);
        // 再次获取驱动
        Driver driver1 = DriverManager.getDriver(url);
    }
}
